﻿using System;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Everis.Recruiting.Support.Extensions
{
    public static class HtmlExtensions
    {
        private static readonly string ApplicationPath = HttpContext.Current.Request.ApplicationPath == "/" ? string.Empty : HttpContext.Current.Request.ApplicationPath;

        public static MvcHtmlString IncludeCss(this HtmlHelper html, string name)
        {
            return MvcHtmlString.Create(string.Format("<link href=\"{0}/Content/styles/{1}.css\" rel=\"stylesheet\" type=\"text/css\"/>", ApplicationPath, name));
        }

        public static MvcHtmlString IncludeJs(this HtmlHelper html, string name)
        {
            return IncludeJs(html, name, false);
        }

        public static MvcHtmlString IncludeJs(this HtmlHelper html, string name, bool defer)
        {
            var deferedString = (defer) ? " defer=\"defer\"" : string.Empty;
            return MvcHtmlString.Create(string.Format("<script src=\"{0}/Content/Scripts/{1}.js\" type=\"text/javascript\"{2}></script>", ApplicationPath, name, deferedString));
        }

        public static MvcHtmlString IncludeBaseJs(this HtmlHelper html, string name)
        {

            return MvcHtmlString.Create(string.Format("<script src=\"{0}/Scripts/{1}.js\" type=\"text/javascript\"></script>", ApplicationPath, name));
        }

        public static MvcHtmlString Image(this HtmlHelper html, string name, int width, int height, string alt)
        {
            return MvcHtmlString.Create(string.Format("<img src=\"{0}/content/images/{1}\" width=\"{2}\" height=\"{3}\" alt=\"{4}\" />", ApplicationPath, name, width, height, alt));
        }

        public static MvcHtmlString Image(this HtmlHelper html, string name, int width, int height, string alt, string id)
        {
            return MvcHtmlString.Create(string.Format("<img id=\"{0}\" src=\"{1}/content/images/{2}\" width=\"{3}\" height=\"{4}\" alt=\"{5}\" />", id, ApplicationPath, name, width, height, alt));
        }

        public static MvcHtmlString ImageEmbedded(this HtmlHelper html, string contentType, string encoding, string data, int width, int height, string alt)
        {
            return MvcHtmlString.Create(string.Format("<img src=\"data:{0};{1},{2}\" width=\"{3}\" height=\"{4}\" alt=\"{5}\" />", contentType, encoding, data, width, height, alt));
        }
        
        public static MvcHtmlString LinkTo<T>(this HtmlHelper html, Expression<Func<T, ActionResult>> action)
        {
            var body = action.Body as MethodCallExpression;
            if (body == null)
            {
                throw new InvalidOperationException("Expression must be a method call");
            }
            var controller = typeof(T).Name.Replace("Controller", string.Empty);
            return MvcHtmlString.Create(string.Format("{0}/{1}/{2}", ApplicationPath, controller, body.Method.Name));
        }

        public static MvcHtmlString Anchor(this HtmlHelper html, string display, string href, string target)
        {
            return MvcHtmlString.Create(string.Format("<a href=\"{0}\" target=\"{1}\">{2}</a>", href, target, display));
        }
   
    }
}